package qlsl.androiddesign.manager.map;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationClientOption.AMapLocationProtocol;
import com.amap.api.location.AMapLocationListener;
import com.tencent.mm.sdk.modelmsg.ShowMessageFromWX;

import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.manager.ActivityManager;
import qlsl.androiddesign.util.runfeng.UIUtils;

/**
 * 高精度定位(新版高德定位SDK)<br/>
 * by qlslylq at 201612061115<br/>
 * 
 */
public class LocationManager {

	private AMapLocationClient locationClient;

	public LocationManager() {
		initLocation();
	}

	private void initLocation() {
		locationClient = new AMapLocationClient(
				SoftwareApplication.getInstance());
		locationClient.setLocationOption(getDefaultOption());
		locationClient.setLocationListener(locationListener);
	}

	private AMapLocationClientOption getDefaultOption() {
		AMapLocationClientOption mOption = new AMapLocationClientOption();
		mOption.setLocationMode(AMapLocationMode.Hight_Accuracy);// 可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
		mOption.setGpsFirst(false);// 可选，设置是否gps优先，只在高精度模式下有效。默认关闭
		mOption.setHttpTimeOut(30000);// 可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
		mOption.setInterval(3000);// 可选，设置定位间隔。默认为2秒
		mOption.setNeedAddress(true);// 可选，设置是否返回逆地理地址信息。默认是true
		mOption.setOnceLocation(false);// 可选，设置是否单次定位。默认是false
		mOption.setOnceLocationLatest(false);// 可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
		mOption.setLocationCacheEnable(true); // 设置是否开启缓存
		AMapLocationClientOption.setLocationProtocol(AMapLocationProtocol.HTTP);// 可选，
																				// 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
		mOption.setSensorEnable(false);// 可选，设置是否使用传感器。默认是false
		return mOption;
	}

	private AMapLocationListener locationListener = new AMapLocationListener() {

		public void onLocationChanged(AMapLocation location) {
			if (location != null) {
				if (location.getErrorCode() == 0) {
					Constant.province = location.getProvince();
					Constant.city = location.getCity().replace("市", "");
					Constant.area = location.getDistrict();
					Constant.latitude = location.getLatitude() + "";
					Constant.longitude = location.getLongitude() + "";
				}
			}
		}
	};

	public void startLocation() {
		locationClient.startLocation();
	}

	public void stopLocation() {
		locationClient.stopLocation();
	}

	public void destroyLocation() {
		if (locationClient != null) {
			locationClient.onDestroy();
			locationClient = null;
		}
	}
}
